---
title: Mutations
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

The [CRUDResolver](./resolvers.mdx) automatically exposes six mutation endpoints. The endpoints names will be derived
from name provided to `@ObjectType` or the class name.

The following examples are based on the following `TodoItemDTO`

```ts title="todo-item.dto.ts"
import { FilterableField, IDField } from '@nestjs-query/query-graphql';
import { ObjectType, ID, GraphQLISODateTime } from '@nestjs/graphql';

@ObjectType('TodoItem')
export class TodoItemDTO {
  @IDField(() => ID)
  id!: string;

  @FilterableField()
  title!: string;

  @FilterableField()
  completed!: boolean;

  @FilterableField(() => GraphQLISODateTime)
  created!: Date;

  @FilterableField(() => GraphQLISODateTime)
  updated!: Date;
}

```

In the following examples you will see the following endpoints referenced

* `createOneTodoItem` - graphql endpoint to create a single record.
* `createManyTodoItems` - graphql endpoint to create multiple records,

* `updateOneTodoItem` - graphql endpoint to update a single record by id.
* `updateManyTodoItems` - graphql endpoint update multiple records with a filter,

* `deleteOneTodoItem` - graphql endpoint to delete one record by id.
* `deleteManyTodoItems` - graphql endpoint to delete multiple records with a filter.

### Create One

The `CRUDResolver` will by default expose a `createOne` mutation using the name of the DTO to name the mutation.

In this example we create a single `TodoItem`, the input by default will be a `Partial` of the DTO.

<Tabs
  defaultValue="graphql"
  values={[
    { label: 'GraphQL', value: 'graphql', },
    { label: 'Response', value: 'response', },
  ]
}>
<TabItem value="graphql">

```graphql
mutation {
  createOneTodoItem(
    input: { todoItem: { title: "Create One Todo Item", completed: false } }
  ) {
    id
    title
    completed
    created
    updated
  }
}
```

</TabItem>
<TabItem value="response">

```json
{
  "data": {
    "createOneTodoItem": {
      "id": "1",
      "title": "Create One Todo Item",
      "completed": false,
      "created": "2020-01-14T09:01:35.834Z",
      "updated": "2020-01-14T09:01:35.834Z"
    }
  }
}
```

</TabItem>
</Tabs>

---

### Create Many

The `CRUDResolver` will by default expose a `createMany` mutation using the name of the DTO to name the mutation.

In this example we create multiple `TodoItems`, the each record is a `Partial` of the DTO.

#### Examples

The following example creates two `TodoItems`.

<Tabs
  defaultValue="graphql"
  values={[
    { label: 'GraphQL', value: 'graphql', },
    { label: 'Response', value: 'response', },
  ]
}>
<TabItem value="graphql">

```graphql
mutation {
  createManyTodoItems(
    input: {
      todoItems: [
        { title: "Create Many Todo Items - 1", completed: false }
        { title: "Create Many Todo Items - 2", completed: true }
      ]
    }
  ) {
    id
    title
    completed
    created
    updated
  }
}
```

</TabItem>
<TabItem value="response">

```json
{
  "data": {
    "createManyTodoItems": [
      {
        "id": "2",
        "title": "Create Many Todo Items - 1",
        "completed": false,
        "created": "2020-01-14T09:01:55.110Z",
        "updated": "2020-01-14T09:01:55.110Z"
      },
      {
        "id": "3",
        "title": "Create Many Todo Items - 2",
        "completed": true,
        "created": "2020-01-14T09:01:55.110Z",
        "updated": "2020-01-14T09:01:55.110Z"
      }
    ]
  }
}
```

</TabItem>
</Tabs>

---

### Update One

The `CRUDResolver` will by default expose an `updateOne` mutation that takes two fields:

* `id`: The id of the record to update.
* `update`: The values to update on the record. This is a partial so you only have to pass in the values you want to
change.

#### Examples

The following example updates the record with `id` equal to `1` to `completed=true`

<Tabs
  defaultValue="graphql"
  values={[
    { label: 'GraphQL', value: 'graphql', },
    { label: 'Response', value: 'response', },
  ]
}>
<TabItem value="graphql">

```graphql
mutation {
  updateOneTodoItem(input: { id: 1, update: { completed: true } }) {
    id
    title
    completed
    created
    updated
  }
}
```

</TabItem>
<TabItem value="response">

```json
{
  "data": {
    "updateOneTodoItem": {
      "id": "1",
      "title": "Create One Todo Item",
      "completed": true,
      "created": "2020-01-14T07:00:31.763Z",
      "updated": "2020-01-14T09:02:28.167Z"
    }
  }
}
```

</TabItem>
</Tabs>

---

### Update Many

The `CRUDResolver` will by default expose an `updateMany` mutation that takes two fields:

* `filter`: The filter to use to find the records to update.
    * **NOTE** The filter **CANNOT** be an empty object. This prevents accidental updating of all records.
* `update`: The values to update on the record. This is a partial so you only have to pass in the values you want to
change.

The response contains the number of records updated.

#### Examples

The following example updates records with an `id` equal to 1 or 2 to `completed=true`.

<Tabs
  defaultValue="graphql"
  values={[
    { label: 'GraphQL', value: 'graphql', },
    { label: 'Response', value: 'response', },
  ]
}>
<TabItem value="graphql">

```graphql
mutation {
  updateManyTodoItems(
    input: { filter: { id: { in: [1, 2] } }, update: { completed: true } }
  ) {
    updatedCount
  }
}
```

</TabItem>
<TabItem value="response">

```json
{
  "data": {
    "updateManyTodoItems": {
      "updatedCount": 2
    }
  }
}
```

</TabItem>
</Tabs>

---

### Delete One

The `CRUDResolver` will by default expose a `deleteOne` mutation that allows you to delete a record by id:

#### Examples

The following example deletes the record with an id equal to 1.

<Tabs
  defaultValue="graphql"
  values={[
    { label: 'GraphQL', value: 'graphql', },
    { label: 'Response', value: 'response', },
  ]
}>
<TabItem value="graphql">

```graphql
mutation {
  deleteOneTodoItem(input: { id: 1 }) {
    id
    title
    completed
    created
    updated
  }
}
```

</TabItem>
<TabItem value="response">

```json
{
  "data": {
    "deleteOneTodoItem": {
      "title": "Create One Todo Item",
      "completed": true,
      "created": "2020-01-14T07:00:31.763Z",
      "updated": "2020-01-14T09:02:51.429Z"
    }
  }
}
```

</TabItem>
</Tabs>

---

### Delete Many

The CRUDResolver will by default expose a `deleteMany` mutation that takes a `filter`:

**NOTE** The filter **CANNOT** be an empty object. This prevents accidental deletion of all records.

#### Examples

The following example deletes all records that start with `Create Many Todo Items`.

<Tabs
  defaultValue="graphql"
  values={[
    { label: 'GraphQL', value: 'graphql', },
    { label: 'Response', value: 'response', },
  ]
}>
<TabItem value="graphql">

```graphql
mutation {
  deleteManyTodoItems(
    input: { filter: { title: { like: "Create Many Todo Items%" } } }
  ) {
    deletedCount
  }
}
```

</TabItem>
<TabItem value="response">

```json
{
  "data": {
    "deleteManyTodoItems": {
      "deletedCount": 6
    }
  }
}
```

</TabItem>
</Tabs>
